package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Join;
import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Tea;
import com.ljc.cjgl.form.TeaForm;
import com.ljc.cjgl.security.SysRole;
import com.ljc.cjgl.security.SysRoleRepository;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.TeaService;

@Controller
@RequestMapping(value="/tea")
public class TeaController {
	@Autowired
	private TeaService service;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private SysRoleRepository roleDAO;
	
	private Specification<Tea> buildSpec(TeaForm form){
		HashSet<Rule> rules=new HashSet<>();
		String search = form.getSearch();
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("username").contain(search));
			rules2.add(Rule.property("name").contain(search));
			rules.add(Rule.or(rules2));
		}
		String code = form.getRoles();
		if(StringUtils.hasText(code)) {
			Join roles=Rule.leftJoin("roles");
			rules.add(Rule.property(roles, "code").equal(code));
		}
		return Rule.and(rules).toSpecification();
		
	}
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		SysUser user=userUtils.getUser();
		List<SysRole> roles=roleDAO.findByCodeNot("ROLE_STUDENT");
		map.put("roles",roles);
		map.put("user",user);
		return "tea/manage";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,TeaForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<Tea> specification = buildSpec(form);
		Page<Tea> page = service.findAll(specification, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	public String edit(Integer id,ModelMap map) {
		Tea model=new Tea();
		if(id!=null) {
			model=service.findById(id);
		}
		List<SysRole> sysRoles=roleDAO.findByCodeNot("ROLE_STUDENT");
		map.put("model",model);
		map.put("roles",sysRoles);
		return "tea/edit";
	}
	
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Object save(TeaForm form) {
		boolean flag=service.save(form);
		if(flag) {
			return TableResult.bulidResult(true, "数据保存成功");
		}else {
			return TableResult.bulidResult(false, "数据保存失败");
		}
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(@RequestBody List<Tea> teas) {
		if(service.delete(teas)) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据删除失败");
		}
	}
	
	
	@RequestMapping(value="/passwordReset")
	@ResponseBody
	@PreAuthorize("hasRole('ROLE_ADMIN')||hasRole('ROLE_DEVELOPER')")
	public Object passwordReset(Integer id) {
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		Tea tea=service.findById(id);
		tea.setPassword(encoder.encode("123456"));
		service.save(tea);
		return "密码复位成功！";
	}
}
